package com.csx.governance.traffic.core.metadata;

import cn.hutool.core.util.StrUtil;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

import java.util.List;
import java.util.Optional;

/**
 * @author cuisongxu
 * @date 2023/3/5 16:01
 */
public class ServiceMetadataLocator {

    private final List<ServiceMetadataProvider> providers;

    public ServiceMetadataLocator(List<ServiceMetadataProvider> providers) {
        this.providers = providers;
        this.providers.sort(AnnotationAwareOrderComparator.INSTANCE);
    }

    public String get(String key) {
        Optional<ServiceMetadataProvider> firstProvider = this.providers.stream().filter(provider -> StrUtil.isNotEmpty(provider.get(key))).findFirst();
        return firstProvider.map(serviceMetadataProvider -> serviceMetadataProvider.get(key)).orElse(null);
    }
}
